var checkPaying;
function payCreate(otherId,orderType) {
    var addressId = 0;
    if (orderType === "BOOK") {
        addressId = $(".addr-selected").attr("data-id");
    }
    var params = {
        "type": 1
        , "payType": "WXPAY"
        , "otherId": otherId
        , "orderType": orderType
        , "addressId": addressId
    }
    orderCreate(params);
}

function orderCreate(params) {
    if (checkPaying) {
        clearInterval(checkPaying);
    }
    //订单提交，服务端要做验证，下单时重新查询数据库
    var loading = layer.load(1);
    $.ajax({
        url: "/order/create.json",
        data: params, type: "post", dataType: "json",
        success: function (result) {
            layer.close(loading);
            if (result.code === 0) {
                //金额页面只作为显示用，以服务端提交时重新取数据库为准
                //判断是否扣款完成
                if (result.msg === 'complete') {
                    return layer.msg("订单扣款成功，正在刷新页面...", {icon: 1, time: 1500}, function () {
                        parent.location.reload();
                    });
                }
                else {
                    var payOptions = result.data;
                    if (params.orderType === "ACCOUNT") {
                        return dialogQrcode(payOptions);
                    }
                    if (payOptions.payChannel === 'JSAPI' && typeof WeixinJSBridge != "undefined") {
                        parent.toWxJSAPI(payOptions);
                    } else if (payOptions.payChannel === 'H5') {
                        parent.location.href = payOptions.payUrl;
                    } else {
                        var options = {
                            render: "canvas",
                            height: 138,
                            width: 138,
                            correctLevel: 0,
                            text: payOptions.payUrl
                        };
                        try {
                            //默认字符串转二维码的方法，仅支持html5
                            $('#qrcode-image').empty().qrcode(options);
                        } catch (e) {
                            //如果不支持html5报错后通过table生成二维码
                            options.render = "table";
                            $('#qrcode-image').empty().qrcode(options);
                        }
                        checkPaying = setInterval("checkPayed('" + payOptions.orderNo + "')", 5000);
                    }
                }
            } else {
                layer.alert(result.msg);
            }
        },
        error: function () {
            layer.close(loading);
            layer.alert("下单异常，请稍后再试");
        },
    });
}

/*  用户中心重新发起支付 */
function orderRepay(orderId, payType) {
    var loading = layer.load(1);
    $.ajax({
        url: "/order/repay.json",
        data: {"orderId": orderId, "payType": payType},
        type: "post", dataType: "json",
        success: function (result) {
            layer.close(loading);
            if (result.code === 0) {
                //判断是否扣款完成
                if (result.msg === 'complete') {
                    return layer.msg("订单扣款成功，正在刷新页面...", {icon: 1, time: 1500}, function () {
                        parent.location.reload();
                    })
                } else {
                    var payOptions = result.data;
                    if (payOptions.payChannel === 'JSAPI') {
                        parent.toWxJSAPI(payOptions);
                    } else if (payOptions.payChannel === 'H5') {
                        window.location.href = payOptions.payUrl;
                    } else {
                        dialogQrcode(payOptions);
                    }
                }
            } else {
                layer.msg(result.msg);
            }
        },
        error: function () {
            layer.close(loading);
            layer.msg("下单异常，请稍后再试");
        }
    });
}

/**
 * 微信二维码支付窗口
 * @param params
 */
function dialogQrcode(params) {
    var loading = layer.load(1);
    var htmlStr = '<div class="qrcode-board">\n' +
        '    <h3 class="qrcode-title">扫一扫付款 <span>￥' + params.amount / 100 + '元</span></h3>\n' +
        '    <div class="qrcode-body">\n' +
        '        <div class="qrcode-image" id="qrcode-image"></div>\n' +
        '        <div class="qrcode-ico">\n' +
        '            <img src="/static/qdxwx/img/weixin.png" style="height: 60px"> 请使用微信扫一扫\n' +
        '        </div>\n' +
        '    </div>\n' +
        '</div>';
    layer.open({
        type: 1,
        title: null,
        content: htmlStr, //这里content是一个普通的String
        cancel: function (index, layero) {
            layer.confirm('您未完成支付操作，确定要关闭窗口吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                layer.closeAll()
                if (location.href.indexOf("/cart/") > 0) {
                    //如果在购物车里，进入订单中心
                    location.href = "/user/order/list.html";
                } else {
                    location.reload();
                }
            }, function () {


            });
            return false;
        }
    });

    var options = {
        render: "canvas",
        height: 258,
        width: 258,
        correctLevel: 0,
        text: params.payUrl
    };
    try {
        //默认字符串转二维码的方法，仅支持html5
        $('#qrcode-image').qrcode(options);
    } catch (e) {
        //如果不支持html5报错后通过table生成二维码
        options.render = "table";
        $('#qrcode-image').qrcode(options);
    }
    setInterval("checkPayed('" + params.orderNo + "')", 5000);
    layer.close(loading);
}

//检查订单是否支付成功
function checkPayed(orderNo) {
    $.ajax({
        url: '/order/pay/check.json',
        data: {"orderNo": orderNo},
        type: 'post', dataType: 'json',
        success: function (result) {
            if (result.code === 0) {
                return layer.msg("订单支付成功，正在刷新页面...", {icon: 1, time: 500}, function () {
                    parent.location.reload();
                })
            }
        }
    });
}